﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for ApprovalService
    /// </summary>
    public class ApprovalService : IHttpHandler
    { 

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            HttpResponse response = context.Response; 
            var model = new OTS_APPROVED();

            if (request["oper"] == "add")
            {
                model.PROJECT_ID = BaseService.PROJECT.ID;
                model.SEQ = Convert.ToInt16(request["SEQ"]);
                model.APPROVED_BY = request["APPROVED_BY"];
                model.POSITION = request["POSITION"];
                model.CREATED = DateTime.Now;
                model.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                var cmd = new Approval_Service(model);
                cmd.Add();
            }
            else
            {
                if (request["oper"] == "del")
                {
                    model.ID = Convert.ToInt16(request["ID"]);
                    var cmd = new Approval_Service(model);
                    cmd.Delete();
                }
                else if (request["oper"] == "edit")
                {
                    model.ID = Convert.ToInt16(request["ID"]);
                    model.SEQ = Convert.ToInt16(request["SEQ"]);
                    model.APPROVED_BY = request["APPROVED_BY"];
                    model.POSITION = request["POSITION"];
                    var cmd = new Approval_Service(model);
                    cmd.Edit();
                }
            }

            var _cmd = new OLTS.DATA_SERVICE.Approval_Service();
            var lst = new List<OTS_APPROVED>();
            var projectid = BaseService.PROJECT.ID;
            var approveList = _cmd.GetAll().Where(x=> x.PROJECT_ID == projectid).ToList();
            foreach (OTS_APPROVED d in approveList)
            {
                model = new OTS_APPROVED();
                model.ID = d.ID;
                model.SEQ = d.SEQ;
                model.APPROVED_BY = d.APPROVED_BY;
                model.POSITION = d.POSITION;
                lst.Add(model);
            }
            lst = lst.OrderBy(o => o.ID).ToList();
           

            int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);
           
            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = lst.Count,
                rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}